package net.zjitc.android2019.note;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import net.zjitc.android2019.R;

import java.util.ArrayList;

/**
 * 首页:
 * 1、图片的点击事件
 * 2、ListView：
 *    1）默认数据展示
 *    2）列表项的点击事件
 *    3）列表项的长按事件
 */
public class NoteActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    ListView listView;
    ImageView image_add;

    NoteBean noteBean;
    NoteAdapter noteAdapter;
    NoteSQLiteHelper helper;
    ArrayList<NoteBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        listView=findViewById(R.id.note_listview);
        image_add=findViewById(R.id.note_image_add);

        /* ListView 默认数据展示*/
        showData();

        /* 点击事件：跳转*/
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NoteActivity.this,RecordActivity.class);
                /* 1、startActivity(intent)的跳转从A到B：
                 *  若B页面要回传数据，还得通过intent.putExtra()
                 *2、startActivityForResult：
                 *  若B页面修改了数据，要回传数据到A页面，
                 *  在A页面中重写onActivityResult方法，就会自动执行*/
                startActivityForResult(intent,1);
            }
        };
        /* 图片绑定事件*/
        image_add.setOnClickListener(listener);
        /*列表项绑定事件*/
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

    }

    /**
     * 重写，自动执行。记录页面做了修改之后，数据回传到首页：
     * ListView数据展示
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==1 && resultCode==2){
                /* ListView 数据的展示 方法*/
                showData();
            }
    }

    /**
     * ListView数据展示:
     *  调用数据库工具类查询出数据list
     *  调用自定义适配器 做数据展示
     */
    private void showData() {
        helper=new NoteSQLiteHelper(NoteActivity.this);
        list=helper.query();
        noteAdapter=new NoteAdapter(NoteActivity.this,list);
        listView.setAdapter(noteAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /*列表项点击之后的操作：传递参数，跳转*/
        noteBean = (NoteBean) listView.getItemAtPosition(position);
        Intent intent = new Intent(NoteActivity.this,RecordActivity.class);
        intent.putExtra("id",noteBean.getId());
        intent.putExtra("content",noteBean.getContent());
        intent.putExtra("time",noteBean.getTime());
        startActivityForResult(intent,1);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
      /*弹对话框*/
        AlertDialog.Builder dialog = new AlertDialog.Builder(NoteActivity.this);
        dialog.setTitle("删除?");
        dialog.setMessage("真的要删除吗？");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /*对话框中确定按钮的操作：从数据库中删除此条记录*/
                /*1)获取当前长按的这条记录
                * 2)调用数据库操作类的删除方法，将该记录删除
                * 3）更新ListView中数据
                * 4）Toast提示删除成功*/
                noteBean= (NoteBean) listView.getItemAtPosition(position);
                if (helper.deleteDate(noteBean.getId())){
                    list.remove(position);
                    /*ListView内容变化后的动态刷新*/
                    noteAdapter.notifyDataSetChanged();
                    Toast.makeText(NoteActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                }

            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
        return false;
    }

}
